package com.mo.httpC;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mo.core.util.MD5Util;

public class MoHttpUtils {
	public static synchronized JSONObject callHttpGet(String url,String key){
		JSONObject json = null;
//		String key = "eyJhbGciOiJIUzUxMiJ9.eyJyYW5kb21LZXkiOiJxcWJqNGgiLCJzdWIiOiJhZG1pbiIsImV4cCI6MTUwNzAyMTgyMiwiaWF0IjoxNTA2NDE3MDIyfQ.YcWPCepb49rDm7yAIc5bIX225w83TMvkSeRW9SVBH0z8ikcqh4f-NZvjHEiOy3-s3prYV5_Bq9cxnSVW8gFBpA";
		try{
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet(url);
			httpGet.setHeader("Authorization", "Bearer "+key);
			CloseableHttpResponse response1 = httpclient.execute(httpGet);
			
			try {
			    System.out.println(response1.getStatusLine());
			    HttpEntity entity1 =  response1.getEntity();
			    
			    String res = EntityUtils.toString(entity1);
			    System.out.println(res);
//			    JSON json1 = (JSON) JSON.toJSON(res);
		    	JSONObject json1 = (JSONObject) JSONObject.parse(res);//fromObject(res);
			    JSONArray arr = json1.getJSONArray("labels");
		    	/*List<VideoLabel> videoLabelList = new ArrayList<VideoLabel>();
			    System.out.println(arr.size());
			    for(int i=0;i<arr.size();i++){
			    	VideoLabel videoLabel = JSON.toJavaObject((JSON) JSON.toJSON(arr.get(i)), VideoLabel.class);
			    	videoLabelList.add(videoLabel);
//			    	System.out.println(arr.get(i));
			    }
			    System.out.println(videoLabelList.size());*/
			    //		    	
//			    json = JSONObject.fromObject(res);//=====
//			    EntityUtils.consume(entity1);//清除结果
			    return json;
			} finally {
			    response1.close();
			    httpclient.close();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static synchronized JSONObject callHttpPost(String url,String token,JSONObject jsonParam){
		JSONObject json = null;
		try{
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);
			httpPost.setHeader("Authorization", "Bearer "+token);
			StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题    
			entity.setContentEncoding("UTF-8");    
			entity.setContentType("application/json");    
			httpPost.setEntity(entity);
			System.out.println();
			CloseableHttpResponse response2 = httpclient.execute(httpPost);

			try {
			    System.out.println(response2.getStatusLine());
			    HttpEntity entity2 = response2.getEntity();
			    String res = EntityUtils.toString(entity2);
//			    json = JSONObject.fromObject(res);
//			    EntityUtils.consume(entity2);//清除结果
			    System.out.println(res);
			    return json;
			} finally {
			    response2.close();
			    httpclient.close();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/*@SuppressWarnings("deprecation")
	public static JSONObject doPost(String url,JSONObject json){
		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		JSONObject response = null;
		try {
			StringEntity s = new StringEntity(json.toString());
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");//发送json数据需要设置contentType
			post.setEntity(s);
			HttpResponse res = client.execute(post);
			if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(res.getEntity());// 返回json格式：
				response = JSONObject.parseObject(result);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return response;
}*/
	
	public static void main(String[] args) {
		getToken();
		
	}
	static void getTo(){
		String url="http://localhost:8280/getIntegral/index";
//		String url="http://localhost:8280/auth";
		String key="eyJhbGciOiJIUzUxMiJ9.eyJyYW5kb21LZXkiOiJkeHEzOHciLCJzdWIiOiJBUEtfT1RUQVVUTzNDNTkxRUJERjgyMSIsImV4cCI6MTUxODA3MTE3OSwiaWF0IjoxNTE3NDY2Mzc5fQ.T4pA5lIdHCAFCwGi1uu5BTctd3fvJq0X4XPPhb_wx7p2ZPFHaabVW56gW0V1p7yGpRZ4SHkEeaCedNx0iRT9yw";
		
		JSONObject jsonParam = new JSONObject();  
//		SimpleObject simpleObject = new SimpleObject();
//		simpleObject.setUser("APK_moFCA386820BE8");
//		jsonParam.put("object", simpleObject);
		Map<String ,Object> map = new HashMap<String,Object>(); 
		
		map.put("userName", "APK_mo3C591EBDF821");
		map.put("integralType",1);
		map.put("dealType", 0);
		map.put("amount", 5);
		map.put("desc", "签到");
		jsonParam.put("object", map);
		//POST请求
		
//        String encrypt = MD5Util.encrypt(jsonParam.getString("object") + "n2j4gt");
		String encrypt = MD5Util.encrypt(JSON.toJSONString(jsonParam.get("object")) + "dxq38w");
		
        System.out.println(encrypt);
		jsonParam.put("sign",encrypt);
		callHttpPost(url, key, jsonParam);
		
		//GET请求
//		String urlget="http://localhost/hello/helloGet?user=admin";
//		callHttpGet(urlget, key);
	}
	static void getToken(){
		String url = "http://localhost:8280/auth?userName=APK_moFCA386820BE8";
		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		JSONObject response = null;
		try {
//			StringEntity s = new StringEntity(json.toString());
//			s.setContentEncoding("UTF-8");
//			s.setContentType("application/json");//发送json数据需要设置contentType
//			post.setEntity(s);
			HttpResponse res = client.execute(post);
			if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(res.getEntity());// 返回json格式：
				response = JSONObject.parseObject(result);
			}
			String token = response.get("token").toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
